package com.softer.chengxin.train.dao.provider;

import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.train.entity.Trainer;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

/**
 * @author Gong Zhiyang
 * @date 2019/1/19
 */
public class TrainerDaoProvider {
    /**
     * 查询项目经理培训人员数量
     *
     * @param map 查询实体
     * @return sql
     */
    public String queryListNum(HashMap<String, Trainer> map) {
        StringBuffer sql = new StringBuffer(20);
        Trainer trainer = map.get("t");
        sql.append("SELECT COUNT(*) FROM project_manager_train WHERE 1=1");
        commonSql(sql, trainer);
        return sql.toString();
    }

    /**
     * 查询项目经理培训人员列表
     *
     * @param map 查询实体
     * @return sql
     */
    public String queryList(HashMap<String, Object> map) {
        StringBuffer sql = new StringBuffer(20);
        Trainer trainer = (Trainer) map.get("t");
        Page page = (Page) map.get("page");
        sql.append("SELECT id,`name`,`code`,company,type,remark FROM project_manager_train WHERE 1=1");
        commonSql(sql, trainer);
        sql.append(" LIMIT ");
        sql.append(page.getBeginIndex()).append(",").append(page.getLimit());
        return sql.toString();
    }

    /**
     * 查询参数拼接接口
     *
     * @param sql     sql
     * @param trainer 查询实体
     */
    private void commonSql(StringBuffer sql, Trainer trainer) {
        if (StringUtils.isNotBlank(trainer.getCode())) {
            sql.append(" AND `code`=#{t.code}");
        }
        if (StringUtils.isNotBlank(trainer.getType())) {
            sql.append(" AND type=#{t.type}");
        }
        if (StringUtils.isNotBlank(trainer.getName())) {
            sql.append(" AND INSTR(`name`,#{t.name})>0");
        }
    }
}
